{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "54d60606",
   "metadata": {},
   "source": [
    "# 本周知识点\n",
    "* 元组，集合和字典的基本概念和常见的方法\n",
    "> 1. 元组\n",
    "> 2. 集合\n",
    "> 3. 字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d23a1f6",
   "metadata": {},
   "source": [
    "## 1. 元组\n",
    "> 1. 概念\n",
    ">> 1. 具有和序列数据相同的运算规则：取值和切片\n",
    ">> 2. 表征：（12345，54321，'hello'),用圆括号/小括号 括起来的，中间仍然是用逗号隔开的数据形态\n",
    "> 2. 性质：不可变性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8278ac4f",
   "metadata": {},
   "outputs": [],
   "source": [
    "product = (\"Head First Python\",88) # 创建了一个元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4cc391d7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Head First Python', 88) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "print(product,type(product))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e0f57f09",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d570beb1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2022, tm_mon=10, tm_mday=31, tm_hour=10, tm_min=20, tm_sec=5, tm_wday=0, tm_yday=304, tm_isdst=0)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0c5184f9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on struct_time object:\n",
      "\n",
      "class struct_time(builtins.tuple)\n",
      " |  struct_time(iterable=(), /)\n",
      " |  \n",
      " |  The time value as returned by gmtime(), localtime(), and strptime(), and\n",
      " |  accepted by asctime(), mktime() and strftime().  May be considered as a\n",
      " |  sequence of 9 integers.\n",
      " |  \n",
      " |  Note that several fields' values are not the same as those defined by\n",
      " |  the C language standard for struct tm.  For example, the value of the\n",
      " |  field tm_year is the actual year, not year - 1900.  See individual\n",
      " |  fields' descriptions for details.\n",
      " |  \n",
      " |  Method resolution order:\n",
      " |      struct_time\n",
      " |      builtins.tuple\n",
      " |      builtins.object\n",
      " |  \n",
      " |  Methods defined here:\n",
      " |  \n",
      " |  __reduce__(...)\n",
      " |      Helper for pickle.\n",
      " |  \n",
      " |  __repr__(self, /)\n",
      " |      Return repr(self).\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Static methods defined here:\n",
      " |  \n",
      " |  __new__(*args, **kwargs) from builtins.type\n",
      " |      Create and return a new object.  See help(type) for accurate signature.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data descriptors defined here:\n",
      " |  \n",
      " |  tm_gmtoff\n",
      " |      offset from UTC in seconds\n",
      " |  \n",
      " |  tm_hour\n",
      " |      hours, range [0, 23]\n",
      " |  \n",
      " |  tm_isdst\n",
      " |      1 if summer time is in effect, 0 if not, and -1 if unknown\n",
      " |  \n",
      " |  tm_mday\n",
      " |      day of month, range [1, 31]\n",
      " |  \n",
      " |  tm_min\n",
      " |      minutes, range [0, 59]\n",
      " |  \n",
      " |  tm_mon\n",
      " |      month of year, range [1, 12]\n",
      " |  \n",
      " |  tm_sec\n",
      " |      seconds, range [0, 61])\n",
      " |  \n",
      " |  tm_wday\n",
      " |      day of week, range [0, 6], Monday is 0\n",
      " |  \n",
      " |  tm_yday\n",
      " |      day of year, range [1, 366]\n",
      " |  \n",
      " |  tm_year\n",
      " |      year, for example, 1993\n",
      " |  \n",
      " |  tm_zone\n",
      " |      abbreviation of timezone name\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data and other attributes defined here:\n",
      " |  \n",
      " |  n_fields = 11\n",
      " |  \n",
      " |  n_sequence_fields = 9\n",
      " |  \n",
      " |  n_unnamed_fields = 0\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Methods inherited from builtins.tuple:\n",
      " |  \n",
      " |  __add__(self, value, /)\n",
      " |      Return self+value.\n",
      " |  \n",
      " |  __contains__(self, key, /)\n",
      " |      Return key in self.\n",
      " |  \n",
      " |  __eq__(self, value, /)\n",
      " |      Return self==value.\n",
      " |  \n",
      " |  __ge__(self, value, /)\n",
      " |      Return self>=value.\n",
      " |  \n",
      " |  __getattribute__(self, name, /)\n",
      " |      Return getattr(self, name).\n",
      " |  \n",
      " |  __getitem__(self, key, /)\n",
      " |      Return self[key].\n",
      " |  \n",
      " |  __getnewargs__(self, /)\n",
      " |  \n",
      " |  __gt__(self, value, /)\n",
      " |      Return self>value.\n",
      " |  \n",
      " |  __hash__(self, /)\n",
      " |      Return hash(self).\n",
      " |  \n",
      " |  __iter__(self, /)\n",
      " |      Implement iter(self).\n",
      " |  \n",
      " |  __le__(self, value, /)\n",
      " |      Return self<=value.\n",
      " |  \n",
      " |  __len__(self, /)\n",
      " |      Return len(self).\n",
      " |  \n",
      " |  __lt__(self, value, /)\n",
      " |      Return self<value.\n",
      " |  \n",
      " |  __mul__(self, value, /)\n",
      " |      Return self*value.\n",
      " |  \n",
      " |  __ne__(self, value, /)\n",
      " |      Return self!=value.\n",
      " |  \n",
      " |  __rmul__(self, value, /)\n",
      " |      Return value*self.\n",
      " |  \n",
      " |  count(self, value, /)\n",
      " |      Return number of occurrences of value.\n",
      " |  \n",
      " |  index(self, value, start=0, stop=9223372036854775807, /)\n",
      " |      Return first index of value.\n",
      " |      \n",
      " |      Raises ValueError if the value is not present.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Class methods inherited from builtins.tuple:\n",
      " |  \n",
      " |  __class_getitem__(...) from builtins.type\n",
      " |      See PEP 585\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(time.localtime())"
   ]
  },
  {
   "cell_type": "raw",
   "id": "33802195",
   "metadata": {},
   "source": [
    "name_seq = 1800001,1800002,1800003\n",
    "name_seq"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ef622d6",
   "metadata": {},
   "source": [
    "# 2.集合\n",
    "> 1. 集合的概念\n",
    ">> 1. 没有重复元素的一个数据结构\n",
    ">> 2. 形态：{'apple','orange','pear','orange','banana'}\n",
    "> 2. 集合的运算\n",
    ">> 1. 交集运算:&，intersection\n",
    ">> 2. 并集运算: untion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f1561978",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'banana', 'orange', 'pear', 'apple'} <class 'set'>\n"
     ]
    }
   ],
   "source": [
    "basket = {'apple','orange','apple','pear','orange','banana'}\n",
    "print(basket,type(basket))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a6cdab31",
   "metadata": {},
   "outputs": [],
   "source": [
    "舞蹈社 = {180001,180003,180004,180005,180007}\n",
    "合唱团 = {180009,18111,180004,180001}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "8a0317ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(舞蹈社)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "642d2d6b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(合唱团)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "fc020524",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{180001, 180004}\n",
      "{180001, 180004}\n"
     ]
    }
   ],
   "source": [
    "#交集\n",
    "print(舞蹈社 & 合唱团)\n",
    "print(舞蹈社.intersection(合唱团))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "52f1f374",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{180001, 180003, 180004, 180005, 180007, 180009, 18111}\n",
      "7\n",
      "{180001, 180003, 180004, 180005, 180007, 180009, 18111}\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "#并集：统计数量\n",
    "print(舞蹈社 | 合唱团)\n",
    "print(len(舞蹈社 | 合唱团))\n",
    "\n",
    "print(舞蹈社.union(合唱团))\n",
    "print(len(舞蹈社.union(合唱团)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd60f825",
   "metadata": {},
   "source": [
    "# 3. 字典\n",
    "> 1.基本的概念\n",
    "> 2.常用的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8841bce5",
   "metadata": {},
   "outputs": [],
   "source": [
    "网新二班 = {\n",
    "    1800001:\"李一峰\",\n",
    "    1800002:\"Jack\",\n",
    "    1800003:\"Marry\",\n",
    "    1800004:\"周杰伦\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "07e32123",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1800001: '李一峰', 1800002: 'Jack', 1800003: 'Marry', 1800004: '周杰伦'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "print(网新二班,type(网新二班))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b1a3f344",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 因为学号是唯一的，可以作为字典的key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1e7490d6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1800001: '李一峰', 1800002: 'Jack', 1800003: 'Marry', 1800004: '周杰伦', 1800005: 'Jack'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "网新三班 = {\n",
    "    1800001:\"李一峰\",\n",
    "    1800002:\"Jack\",\n",
    "    1800003:\"Marry\",\n",
    "    1800004:\"周杰伦\",\n",
    "    1800005:\"Jack\"\n",
    "}\n",
    "print(网新三班,type(网新三班))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "17549bca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'周杰伦'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字典的取值：通过keys 进行字典的取值，unlike 列表、元组和字符串通过index取值\n",
    "网新三班[1800004]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6a06f2b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "网新三班 = {\n",
    "    1800001:[\"李一峰\",\"广州\",\"男\",19,\"聊天记录\",\"朋友圈\"],\n",
    "    1800002:\"Jack\",\n",
    "    1800003:\"Marry\",\n",
    "    1800004:\"周杰伦\",\n",
    "    1800005:\"Jack\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ce9ca9c4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['李一峰', '广州', '男', 19, '聊天记录', '朋友圈']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新三班[1800001]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0fc98269",
   "metadata": {},
   "source": [
    "# 3.2 字典的一般方法\n",
    ">   1. 按照字典的形式直接创建(key:value)\n",
    "网新二班 = {\n",
    "    1800001:\"李一峰\",\n",
    "    1800002:\"Jack\",\n",
    "    1800003:\"Marry\",\n",
    "    1800004:\"周杰伦\"\n",
    "}\n",
    "> 2.使用dict()方法创建字典\n",
    "网新四班 = dict(\n",
    "    stu1800001=\"李一峰\",\n",
    "    stu1800002=\"Jack\",\n",
    "    stu1800003=\"Marry\",\n",
    "    stu1800004=\"周杰伦\",\n",
    "\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b9e89374",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用dict()方法创建字典\n",
    "网新四班 = dict(\n",
    "    stu1800001=\"李一峰\",\n",
    "    stu1800002=\"Jack\",\n",
    "    stu1800003=\"Marry\",\n",
    "    stu1800004=\"周杰伦\",\n",
    "\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5bcd6502",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1800001': '李一峰',\n",
       " 'stu1800002': 'Jack',\n",
       " 'stu1800003': 'Marry',\n",
       " 'stu1800004': '周杰伦'}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新四班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "966e2ae9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用字典增加的方式创建字典 : dict[key] = value\n",
    "\n",
    "网新一班 = {}\n",
    "网新一班[\"stu1800001\"] = '李一峰'\n",
    "网新一班[\"stu1800002\"] = 'Jack'\n",
    "网新一班[\"stu1800003\"] = 'Marry'\n",
    "网新一班[\"stu1800004\"] = '周杰伦'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a4c181ca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1800001': '李一峰',\n",
       " 'stu1800002': 'Jack',\n",
       " 'stu1800003': 'Marry',\n",
       " 'stu1800004': '周杰伦'}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新一班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c01d0ba0",
   "metadata": {},
   "outputs": [],
   "source": [
    "网新一班_keys = [\"stu1800001\",\"stu1800002\",\"stu1800003\",\"stu1800004\"]\n",
    "网新一班_values = ['李一峰','Jack','Marry','周杰伦']\n",
    "网新一班 = {}\n",
    "for i in range(4):\n",
    "    网新一班[网新一班_keys[i]] = 网新一班_values[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a84a452e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1800001': '李一峰',\n",
       " 'stu1800002': 'Jack',\n",
       " 'stu1800003': 'Marry',\n",
       " 'stu1800004': '周杰伦'}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新一班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d1c46a1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
